<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%--<%@ page import="com.iWen.survey.dao.*" %>
<%@ page import="com.iWen.survey.dto.*" %>
<%@ page import="com.iWen.survey.util.*" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%
ConfigDAO cdao=DAOFactory.getConfigDAO();
Config cfg=cdao.findConfig();
SurveyDAO sdao=DAOFactory.getSurveyDAO();
Long sid=Long.valueOf(request.getParameter("sid"));
Survey survey=sdao.findSurvey(sid);
if(survey==null){
	out.println("对不起，没有这个编号的问卷！");
	return;
}
//问卷开放性校验
if(survey.getSIsAudited()==false){
	out.println("该问卷未通过审核！");
	return;
}
//问卷过期时间校验
if(new java.util.Date().after(survey.getSExpireDate())){
	out.println("该问卷已经因过期而失效！");
	return;
}
%>--%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <meta http-equiv="keywords" content=""/>
    <meta http-equiv="description" content=""/>
    <title>iWen问卷调查系统</title>
    <link rel="stylesheet" href="Css/Style.css" type="text/css"/>
    <%--<script type="text/javascript" src="Js/prototype.js"></script>--%>
    <script type="text/javascript" src="Js/jquery-1.11.1.min.js"></script>
    <script type="text/javascript" src="Js/json2.min.js"></script>
</head>
<body>
<div id="head">
    <img src="images/head.jpg;" style="width:770px;height:140px"/>
</div>
<script type="text/javascript">
    //学生参加考试
    $(function () {
        //是否结束
        //var flag = false;
        //绑定提交按钮
        $("#submit-btn").click(submit);
        //监听窗口关闭或刷新事件，如果还没有做完，给用户一个提示
        /*window.onbeforeunload = function () {
            if (!flag) {
                window.event.returnValue = "考试尚未结束，您确定离开?";
            }
        };*/

        /**
         * 交卷
         * 提交的json串的格式:
         * {
	 * 		eid: 1,//试卷id
	 * 		questions:[
	 * 			{id: 2, answer: "1,2"}
	 * 		]
	 * }
         */
        function submit() {
            var result = {};
            result.eid = $("#survey-id").val();
            result.questions = [];
            //处理单选题
            var $singles = $("#single-container").find("div.question"), id, answer, $question, $checkeds;
            $.each($singles, function (index, question) {
                $question = $(question);
                id = $question.find("input[name=question-id]").val();
                //如果没有选中，那么
                $checkeds = $question.find("input:checked");
                answer = $checkeds.length > 0 ? $checkeds.val() : "";
                result.questions.push({
                    id: id,
                    type: 1,
                    answer: answer
                });
            });
            //处理多选题
            $("#multi-container").find("div.question").each(function (index, question) {
                $question = $(question);
                id = $question.find("input[name=question-id]").val();
                //拼接答案串
                answer = "";
                $question.find("input:checked").each(function () {
                    answer += (this.value + ",");
                });
                result.questions.push({
                    id: id,
                    type: 2,
                    answer: answer.substring(0, answer.length - 1)
                });
            });
            $.post("Surveysubmit", "result=" + JSON.stringify(result), function (data) {
                if (data.code != 200) {
                    alert(data.msg);
                    setTimeout(function () {
                        window.location.href = "index";
                    }, 2000);
                } else {
                    setTimeout(function () {
                        window.location.href = "success.jsp";
                    }, 2000);
                }
            }, "json");
        }

    });

    function say(id) {
        $.post(
            "/say",
            "id="+id,
            function (data) {

            },"json"
        );
    }
</script>
<div id="surveyBox">
    <div class="center">
            <div class="name"><b>${survey.name}</b><br/>
                <font size=2>本问卷题目数量上限为：${count} 题</font><br/>
                <c:if test="${survey.img!=null}">
                    <img src=UploadImg/${survey.img} height="250" width="300"/>
                </c:if>
            </div>
            <div class="desc">${survey.desc}</div>
            <input type="hidden" id="survey-id" value="${sid}"/>
        <div class="Question_list">
            <div id="single-container">
            <c:forEach items="${survey.singleQuestions}" var="question" varStatus="status">
                    <div class="question-title">
                            ${status.index + 1}.&nbsp;&nbsp;${question.head}
                        &ensp;&ensp;&ensp;&ensp;
                        <input id="say" type="button" onclick="say(${question.id})" value="语音"/>
                    </div>
                            <div class="question">
                                <input type="hidden" name="question-id" value="${question.id}"/>
                        <div class="question-answer">
                                <ol>
                                    <c:if test="${question.optionA!=null}">
                                        <input type="radio" name="single-${status.index}" value="0"/>${question.optionA}<br>
                                    </c:if>
                                    <c:if test="${question.optionB!=null}">
                                        <input type="radio" name="single-${status.index}" value="1"/>${question.optionB}<br>
                                    </c:if>
                                    <c:if test="${question.optionC!=null}">
                                        <input type="radio" name="single-${status.index}" value="2"/>${question.optionC}<br>
                                    </c:if>
                                    <c:if test="${question.optionD!=null}">
                                        <input type="radio" name="single-${status.index}" value="3"/>${question.optionD}<br>
                                    </c:if>

                                </ol>
                        </div>
                            </div>
            </c:forEach>
                        </div>
                        <div id="multi-container">
                        <c:forEach items="${survey.multiQuestions}" var="question" varStatus="status">
                            <div class="question">
                                <div class="question-title">
                                        ${status.index + 1}.&nbsp;&nbsp;${question.head}
                                            <input id="say" type="button" onclick="say(${question.id})" value="语音"/>
                                </div>
                                <input type="hidden" name="question-id" value="${question.id}"/>
                                <div class="question-answer">
                                        <ol>
                                            <input type="checkbox" value="0"/>${question.optionA}<br>
                                            <input type="checkbox" value="1"/>${question.optionB}<br>
                                            <input type="checkbox" value="2"/>${question.optionC}<br>
                                            <input type="checkbox" value="3"/>${question.optionD}<br>

                                        </ol>
                                </div>
                            </div>
                        </c:forEach>
                        </div>
        </div>

            <div class="button">
                <button id="submit-btn">提交</button>
            </div>
            <div class="backindex">
                <a href="index" target="_self">&nbsp;返回首页</a>
            </div>
    </div>
</div>
<%--<div id="bottom">
<%=cfg.getCSiteName() %><br/>
<%=cfg.getCopyright() %> 
</div><!--bottom end-->--%>
</body>
</html>
